package com.ccmusic.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ccmusic.Message.Msg;
import com.ccmusic.entity.User;
import com.ccmusic.service.UserService;
import com.ccmusic.service.impl.UserServiceImpl;
import com.ccmusic.util.DatatablesBean;
import com.google.gson.Gson;
/**
 * wwb 2019.3.28
 */
/**
 * Servlet implementation class CcmusicServlet
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
	UserService us = new UserServiceImpl();
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op="";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if(op.equals("getAllUser")) {
			//System.out.println("**************************");
			//System.out.println("客户端请求分页查询所有用户,本查询使用逻辑分页");
			//System.out.println("**************************");
			getAllUser(request,response);
		}else if(op.equals("insertUser")) {
			//System.out.println("**************************");
			//System.out.println("客户端请求增加用户");
			//System.out.println("**************************");
			insertUser(request,response);
		}else if(op.equals("deleteUser")) {
			//System.out.println("**************************");
			//System.out.println("客户端请求删除用户");
			//System.out.println("**************************");
			deleteUser(request,response);
		}else if(op.equals("updateUser")) {
			//System.out.println("**************************");
			//System.out.println("客户端请求修改用户信息");
			//System.out.println("**************************");
			updateUser(request,response);
			//主页点击个人信息修改用户信息  奥术大师著
		}else if(op.equals("update")) {
			update(request,response);
		}
	}
	//用户在个人信息修改
	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		//响应输出流
		PrintWriter out=response.getWriter();
		
		//获取客户端发送的请求数据
		String uname=request.getParameter("uname");
		//System.out.println("uname:"+uname);
		String introduce=request.getParameter("intr");
		String birthday=request.getParameter("birthday");
		String usex=request.getParameter("usex");
		int uid=Integer.parseInt(request.getParameter("uid"));
		//创建用户对象
		User user=new User(uid,uname, usex, birthday, introduce);
		boolean result=us.update(user);
		//System.out.println(user);
		String mes=result?"修改成功":"修改失败";
		
		Msg mess=new Msg();
		mess.setMsg(mes);
		//System.out.println("我有进来update");
		Gson gson=new Gson();
		
		out.println(gson.toJson(mess));
		out.close();
		
	}

	private void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		//响应输出流
		PrintWriter out=response.getWriter();
		
		//获取客户端发送的请求数据
		int uid=Integer.parseInt(request.getParameter("uid"));
		String uname=request.getParameter("uname");
		String ulevel=request.getParameter("ulevel");
		int ustatus=Integer.parseInt(request.getParameter("ustatus"));
		String regdate=request.getParameter("regdate");
		
		//创建用户对象
		User user=new User(uid, uname, ulevel, ustatus, regdate);
		//System.out.println(user);
		boolean result=us.updateInfo(user);
		
		String mes=result?"修改成功":"修改失败";
		
		Msg mess=new Msg();
		mess.setMsg(mes);
		
		Gson gson=new Gson();
		
		out.println(gson.toJson(mess));
		out.close();
		
	}

	private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		
		//得到输出流
		PrintWriter out=response.getWriter();
		
		//获取客户端发送过来的请求参数
		
		int uid=Integer.parseInt(request.getParameter("uid"));
		
		boolean result=us.deleteUser(uid);
		
		String mes=result?"删除成功":"删除失败";
		
		//创建信息对象
		Msg mess=new Msg();
		
		mess.setMsg(mes);
		
		Gson gson=new Gson();
		
		out.println(gson.toJson(mess));
		
		out.close();
		
	}

	private void insertUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		//得到输出流
		
		PrintWriter out=response.getWriter();
		
		//获取客户端发送过来的参数
		String uname=request.getParameter("uname");
		String pwd=request.getParameter("pwd");
		String ulevel=request.getParameter("ulevel");
		int ustatus=Integer.parseInt(request.getParameter("ustatus"));
		
		//创建用户对象
//		User user=new User(uname,pwd, ulevel, ustatus);
		User user = new User();
		user.setUname(uname);
		user.setPwd(pwd);
		user.setUlevel(ulevel);
		user.setUstatus(ustatus);
		
		//将增加的用户添加至数据库
		boolean result=us.register(user);
		
		String mes="添加失败";
		//创建信息对象
		Msg mess=new Msg();
		
		if(result) {
			mes="添加成功";			
		}
		mess.setMsg(mes);
		
		//将信息对象转成json格式
		Gson gson=new Gson();
		String data=gson.toJson(mess);
		out.println(data);
		out.close();
	}

	private void getAllUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json");
		PrintWriter out=response.getWriter();
	
		DatatablesBean<User> db=us.getUserByAjaxDt();
		Gson gson=new Gson();
		String data=gson.toJson(db);
		out.print(data);
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
